//
//  KYBubbleView.m
//  DrugRef
//
//  Created by chen xin on 12-6-6.
//  Copyright (c) 2012年 Kingyee. All rights reserved.
//

#import "AppsBubbleAnnotationAccessoryView.h"
#import "CommonUtility.h"

@implementation AppsBubbleAnnotationAccessoryView

static const float kBorderWidth = 10.0f;
static const float kEndCapWidth = 10.0f; //20.0f
static const float kMaxLabelWidth = 260.0f;

@synthesize delegate = _delegate;
@synthesize dataInfo = _dataInfo;
@synthesize index;

#pragma mark - Private

- (void)rightButtonAction:(id)sender {
    BOOL hiddenAccessoryView = [[_dataInfo objectForKey:@"hiddenAccessory"] boolValue];
    if(hiddenAccessoryView){
        return;
    }
    if(self.delegate && [self.delegate respondsToSelector:@selector(appsBubbleAnnotationAccessoryView:didSelect:)]){
        [self.delegate appsBubbleAnnotationAccessoryView:self didSelect:index];
    }
}

#pragma mark - Public

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        titleLabel = [[UILabel alloc] init];
        titleLabel.backgroundColor = [UIColor clearColor];
        [titleLabel setTextColor:[UIColor whiteColor]];
        titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        titleLabel.numberOfLines = 0;
        [self addSubview:titleLabel];
        
        detailLabel = [[UILabel alloc] init];
        detailLabel.backgroundColor = [UIColor clearColor];
        [detailLabel setTextColor:[UIColor whiteColor]];
        detailLabel.numberOfLines = 0;
        detailLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        [self addSubview:detailLabel];

        rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightButton setFrame:CGRectMake(0, 0, 25, 25)];
        [rightButton setImage:[UIImage imageNamed:@"mapapi.bundle/images/icon-arrow.png"] forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:rightButton];
        
        touchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [touchButton setFrame:CGRectMake(0, 0, 0, 0)];
        [touchButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:touchButton];
        
        UIImage *imageNormal, *imageHighlighted;
        imageNormal = [[UIImage imageNamed:@"mapapi.bundle/images/icon-paopao-left.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:13];
        UIImageView *leftBgd = [[UIImageView alloc] initWithImage:imageNormal highlightedImage:imageHighlighted];
        leftBgd.tag = 11;
        imageNormal = [[UIImage imageNamed:@"mapapi.bundle/images/icon-paopao-right.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:13];
        UIImageView *rightBgd = [[UIImageView alloc] initWithImage:imageNormal highlightedImage:imageHighlighted];
        rightBgd.tag = 12;
        
        [self addSubview:leftBgd];
        [self sendSubviewToBack:leftBgd];
        [self addSubview:rightBgd];
        [self sendSubviewToBack:rightBgd];
    }
    return self;
}

- (BOOL)showFromRect:(CGRect)rect type:(AccessoryViewType)accessoryViewType {
    if(!_dataInfo) {
        return NO;
    }
    
    
    
    BOOL hiddenAccessoryView = [[_dataInfo objectForKey:@"hiddenAccessory"] boolValue];
    
    [rightButton setHidden:hiddenAccessoryView];
    
    NSString *name = [_dataInfo objectForKey:@"name"];
    NSString *detail = [_dataInfo objectForKey:@"address"];
    
    if ([name isEqualToString:@""] && [detail isEqualToString:@""]) {
        self.frame = CGRectZero;
    }else{
        titleLabel.text = name;
        detailLabel.text = detail;
        
        titleLabel.frame = CGRectZero;
        CGRect rect1 = titleLabel.frame;
        
        detailLabel.frame = CGRectZero;
        CGRect rect2 = detailLabel.frame;
        
        CGRect rect0 = self.frame;
        
        CGFloat longWidth = 0.0f;
        
        
        switch (accessoryViewType) {
            case 0:{
                [titleLabel setHidden:NO];
                [detailLabel setHidden:NO];
                
                rect1.origin = CGPointMake(kBorderWidth, kBorderWidth);
                
                CGSize size1 = [name sizeWithFont:detailLabel.font constrainedToSize:CGSizeMake(kMaxLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
                rect1.size = size1;
                
                titleLabel.frame = CGRectMake(rect1.origin.x, rect1.origin.y - 4, rect1.size.width,rect1.size.height);
                
                
                rect2.origin.x = kBorderWidth;
                rect2.origin.y = rect1.size.height + 5;
                
                CGSize size = [detail sizeWithFont:detailLabel.font constrainedToSize:CGSizeMake(kMaxLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
                rect2.size = size;
                detailLabel.frame = rect2;
                
                longWidth = (rect1.size.width > rect2.size.width) ? rect1.size.width : rect2.size.width;
                
                rect0.size.height = rect1.size.height + rect2.size.height + 2*kBorderWidth + kEndCapWidth;
                
                
                break;
            }
            case 1:{
                [titleLabel setHidden:NO];
                [detailLabel setHidden:YES];
                
                rect1.origin = CGPointMake(kBorderWidth, kBorderWidth);
                CGSize size1 = [name sizeWithFont:titleLabel.font constrainedToSize:CGSizeMake(kMaxLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
                rect1.size = size1;
                titleLabel.frame = CGRectMake(rect1.origin.x, rect1.origin.y - 4, rect1.size.width,rect1.size.height);
                
                longWidth = rect1.size.width;
                
                rect0.size.height = rect1.size.height + 2*kBorderWidth + kEndCapWidth;
                
                
                break;
            }
            case 2:{
                [titleLabel setHidden:YES];
                [detailLabel setHidden:NO];
                
                rect2.origin = CGPointMake(kBorderWidth, kBorderWidth);
                
                CGSize size = [detail sizeWithFont:detailLabel.font constrainedToSize:CGSizeMake(kMaxLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
                rect2.size = size;
                
                detailLabel.frame = CGRectMake(rect2.origin.x, rect2.origin.y - 4, rect2.size.width,rect2.size.height);
                
                longWidth = rect2.size.width;
                
                rect0.size.height = rect2.size.height + 2*kBorderWidth + kEndCapWidth;
                break;
            }
            case 3:{
                [titleLabel setHidden:NO];
                [detailLabel setHidden:NO];
                
                [titleLabel setNumberOfLines:1];
                [detailLabel setNumberOfLines:1];
                
                longWidth = kMaxLabelWidth;
                CGFloat titleWidth = fitLabelWidth(titleLabel);
                CGFloat detailTitleWidth = fitLabelWidth(detailLabel);
                
                if (titleWidth > detailTitleWidth) {
                    
                    if (titleWidth<kMaxLabelWidth) {
                        longWidth = titleWidth;
                    }else{
                        titleWidth = kMaxLabelWidth;
                    }
                }else{
                    if (detailTitleWidth<kMaxLabelWidth) {
                        longWidth = detailTitleWidth;
                    }else{
                        detailTitleWidth = kMaxLabelWidth;
                    }
                }
                
                rect1.origin = CGPointMake(kBorderWidth, kBorderWidth);
                rect1.size.height = 21.0f;
                rect1.size.width = titleWidth;
                titleLabel.frame = rect1;
                rect2.origin.x = kBorderWidth;
                rect2.size.height = 21.0f;
                rect2.size.width = detailTitleWidth;
                rect2.origin.y = rect1.size.height + 5;
                detailLabel.frame = rect2;
                rect0.size.width =
                rect0.size.height = rect1.size.height + rect2.size.height + 2*kBorderWidth + kEndCapWidth;
                
                
            }
            default:
                break;
        }
        
        rect0.size.width = longWidth + 2*kBorderWidth;
        rect0.size.width = rect0.size.width + (hiddenAccessoryView ? 0 : 35); //35
        
        rightButton.frame = CGRectMake(rect0.size.width - rightButton.frame.size.width - 8,rect0.size.height / 2 - rightButton.frame.size.height / 2 - 8,rightButton.frame.size.width,rightButton.frame.size.height);
        touchButton.frame = CGRectMake(0,0,rect0.size.width,rect0.size.height);
        self.frame = rect0;
        
        CGFloat halfWidth = rect0.size.width/2;
        UIView *image = [self viewWithTag:11];
        CGRect iRect = CGRectZero;
        iRect.size.width = halfWidth;
        iRect.size.height = rect0.size.height;
        image.frame = iRect;
        image = [self viewWithTag:12];
        iRect.origin.x = halfWidth;
        image.frame = iRect;

    }
    
        
    return YES;
}

@end
